import 'package:dio/dio.dart';
import 'package:dio_cookie_manager/dio_cookie_manager.dart';
import 'package:flutterdemo/common/config/appConstant.dart';
import 'package:flutterdemo/common/data/net3/DioInterceptors.dart';
import 'package:flutterdemo/common/data/net3/ResultCode.dart';
import 'package:cookie_jar/cookie_jar.dart';

class DioManger {
  static DioManger _instance;

  static DioManger getInstance() {
    if (_instance == null) {
      _instance = new DioManger();
    }
    return _instance;
  }

  Dio dio = new Dio();

  DioManger() {
    dio.options.baseUrl = AppConstant.baseUrl;
    dio.options.connectTimeout = 5000;
    dio.options.receiveTimeout = 3000;
    dio.interceptors.add(new DioInterceptors());
    // dio.interceptors.add(LogInterceptor(responseBody: true)); //开启请求日志
    // setCookieManager();
  }

  void setCookieManager() {
    var cookieJar = CookieJar();
    dio.interceptors.add(CookieManager(cookieJar));
  }

  get(String url, Map params, Map<String, String> headers,
      Function successCallBack, Function errorCallBack) async {
    _requestHttp(url, headers, successCallBack, 'get', params, errorCallBack);
  }

  post(String url, params, Map<String, String> headers,
      Function successCallBack, Function errorCallBack) async {
    _requestHttp(url, headers, successCallBack, 'post', params, errorCallBack);
  }

  _requestHttp(
      String url, Map<String, String> headers, Function successCallBack,
      [String method, Map params, Function errorCallBack]) async {
    Response response;
    try {
      if (headers != null) {
        ///显示指定Map的限定类型 动态添加headers
        dio.options.headers.addAll(new Map<String, String>.from(headers));
      }
      CancelToken token = CancelToken();
      if (method == 'get') {
        if (params != null && params.isNotEmpty) {
          response =
              await dio.get(url, queryParameters: params, cancelToken: token);
        } else {
          response = await dio.get(url, cancelToken: token);
        }
      } else if (method == 'post') {
        if (params != null && params.isNotEmpty) {
          response = await dio.post(url, data: params, cancelToken: token);
        } else {
          response = await dio.post(url, cancelToken: token);
        }
      }
      // debug模式打印相关数据
      if (AppConstant.openBug) {
        print('请求url: ' + url);
        print('请求头: ' + dio.options.headers.toString());
        if (params != null) {
          print('请求参数: ' + params.toString());
        }
        if (response != null) {
          print('请求结果: ' + response.toString());
        }
      }
      successCallBack(response.toString(), token, response);
    } on DioError catch (error) {
      // 请求错误处理
      Response errorResponse;
      if (error.response != null) {
        errorResponse = error.response;
      } else {
        errorResponse =
            new Response(statusCode: 666, requestOptions: error.requestOptions);
      }
      // 请求超时
      if (error.type == DioErrorType.connectTimeout) {
        errorResponse.statusCode = ResultCode.CONNECT_TIMEOUT;
      }
      // 一般服务器错误
      else if (error.type == DioErrorType.receiveTimeout) {
        errorResponse.statusCode = ResultCode.RECEIVE_TIMEOUT;
      }
      // debug模式才打印
      if (AppConstant.openBug) {
        print('请求头: ' + dio.options.headers.toString());
        print('method: ' + dio.options.method);
        print('请求异常url: ' + url);
        print('请求异常: ' + error.toString());
      }
      _error(errorCallBack, error.message);
    }
  }

  _error(Function errorCallBack, String error) {
    if (errorCallBack != null) {
      errorCallBack(error);
    }
  }
}
